/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.sysdrive.DAO;

import br.com.sysdrive.model.Cargo;
import br.com.sysdrive.util.ConnectionFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Rodrigo
 */
public class CargoDAOImpl implements GenericDAO {

    private Connection conn;
    private ResultSet rs;

    public CargoDAOImpl() throws Exception {
        try {
            this.conn = ConnectionFactory.getConnection();
            System.out.println("Conectado com Sucesso!");
        } catch (Exception ex) {
            throw new Exception(ex.getMessage());
        }
    }

    @Override
    public Boolean cadastrar(Object object) {
        PreparedStatement stmt = null;
        Cargo cargo = (Cargo) object;
        String sql = "insert into cargo(descricao_cargo, status_cargo) values (?,?);";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, cargo.getDescricaoCargo().toUpperCase());
            stmt.setString(2, cargo.getStatusCargo().toUpperCase());
            stmt.execute();
            return true;
        } catch (SQLException ex) {
            System.out.println("Problemas ao cadastrar Cargo! Erro: " + ex.getMessage());
            ex.printStackTrace();
            return false;
        } finally {
            try {
                ConnectionFactory.closeConnection(conn, stmt);
            } catch (Exception ex) {
                System.out.println("Problemas ao fechar os parametros de conexão! Erro: " + ex.getMessage());
                ex.printStackTrace();
            }
        }
    }

    @Override
    public List<Object> listar() {
        PreparedStatement stmt = null;
        ResultSet rs = null;
        List<Object> resultado = new ArrayList<Object>();
        String sql = "select c.* from cargo c order by c.descricao_cargo";

        try {
            stmt = conn.prepareStatement(sql);
            rs = stmt.executeQuery();
            while (rs.next()) {
                Cargo cargo = new Cargo();
                cargo.setIdCargo(rs.getInt("id_cargo"));
                cargo.setDescricaoCargo(rs.getString("descricao_cargo"));
                cargo.setStatusCargo(rs.getString("status_cargo"));

                resultado.add(cargo);
            }
        } catch (SQLException ex) {
            System.out.print("Problemas ao listar Cargo! Erro: " + ex.getMessage());
            ex.printStackTrace();
        } finally {
            try {
                ConnectionFactory.closeConnection(conn, stmt, rs);
            } catch (Exception ex) {
                System.out.println("Problemas ao fechar os parâmetros de conexão! Erro: " + ex.getMessage());
                ex.printStackTrace();
            }
            return resultado;
        }
    }

    @Override
    public Object carregar(int idObject) {

        PreparedStatement stmt = null;
        ResultSet rs = null;
        Cargo cargo = null;

        String sql = "select c.* from cargo c where id_cargo=?";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, idObject);
            rs = stmt.executeQuery();

            while (rs.next()) {
                cargo = new Cargo();
                cargo.setIdCargo(rs.getInt("id_cargo"));
                cargo.setDescricaoCargo(rs.getString("descricao_cargo"));
                cargo.setStatusCargo(rs.getString("status_cargo"));
            }
            return cargo;
        } catch (SQLException ex) {
            System.out.print("Problemas ao carregar Cargo! Erro: " + ex.getMessage());
            ex.printStackTrace();
            return null;
        } finally {
            try {
                ConnectionFactory.closeConnection(conn, stmt, rs);
            } catch (Exception ex) {
                System.out.println("Problemas ao fechar os parâmetros de conexão! Erro: " + ex.getMessage());
                ex.printStackTrace();
            }
        }
    }

    @Override
    public Boolean alterar(Object object) {
        PreparedStatement stmt = null;
        Cargo cargo = (Cargo) object;
        String sql = "update cargo set descricao_cargo = ?, status_cargo = ?"
                + " where id_cargo = ?";
        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, cargo.getDescricaoCargo().toUpperCase());
            stmt.setString(2, cargo.getStatusCargo().toUpperCase());
            stmt.setInt(3, cargo.getIdCargo());
            stmt.executeUpdate();
            return true;
        } catch (SQLException ex) {
            System.out.println("Problemas no SQL ao alterar Cargo! Erro CargoDAO: " + ex.getMessage());
            return false;
        } finally {
            try {
                ConnectionFactory.closeConnection(conn, stmt);
            } catch (Exception ex) {
                System.out.println("Problemas ao fechar os parametros de conexão Alterar Cargo! Erro CargoDAO:" + ex.getMessage());
            }
        }
    }
}
